Output: 321
Method 1 : Using Recursion | Level : Intermediate | Best for : Interviews
# Author: P.Prabhas
# Reverse a number using recursion (simple version)
def reverse_number(num, rev):
# Step 1: Stop when number becomes 0
if num == 0:
return rev
# Step 2: Take last digit
last_digit = num % 10
# Step 3: Add digit to reversed number
rev = rev * 10 + last_digit
# Step 4: Recursive call
return reverse_number(num // 10, rev)
# Step 5: Take input from user
num = int(input("Enter a number: "))
# Step 6: Call function
result = reverse_number(num, 0)
# Step 7: Print result
print("Reversed number is:", result)
Enter a number: 123
Reversed number: 321
How it works :
First we define a function called reverse_number. this function takes two values. one is the number and the other is the reversed value which starts from 0.
Inside the function, we first check whether the number is 0. If it is 0, we return the reversed value. this is the base condition that stops the recursion.
If the number is not 0, we take the last digit using num% 10. this gives us one digit at a time from the right side.
Then we add that digit to the reversed value. before adding we multiply the current reversed value by 10 so that the digit shifts to the left and makes space for the new digit.
After that we call the same function again with the remaining number using num // 10.
This process continues again and again until the number becomes 0.
Finally the reversed number is returned.
This works because each function call processes one digit and passes the remaining digits to the next call.
Method 2 : Using Recursion with Negative Numbers | Level : Important | Best for : Real programs
This method handles both positive and negative numbers.
Code :
# Author: P.Prabhas
# Function to reverse number using recursion
def reverse_number(num, rev):
# Base condition
if num == 0:
return rev
# Extract last digit
digit = num % 10
# Build reversed number
rev = rev * 10 + digit
# Recursive call
return reverse_number(num // 10, rev)
# Take input
num = int(input("Enter a number: "))
# Store sign
sign = 1
if num < 0:
sign = -1
num = abs(num)
# Reverse number
result = reverse_number(num, 0)
# Apply sign and print result
print("Reversed number:", sign * result)
Output:Enter a number: -123
Reversed number: -321How it works :
First we take a number from the user. then we check whether the number is negative.
If the number is negative, we store the sign separately and convert the number into a positive value using abs().
Then we apply the recursion logic to reverse the number.
After reversing, we multiply the result with the stored sign.
This ensures correct result for both positive and negative numbers.
Method 3 : Using Helper Function | Level : Intermediate | Best for : Clean structure
Code :# Author: P.Prabhas # Helper function for recursion def helper(num, rev): # Base condition if num == 0: return rev # Extract last digit digit = num % 10 # Build reversed number rev = rev * 10 + digit # Recursive call return helper(num // 10, rev) # Main function def reverse_number(num): # Call helper function return helper(num, 0) # Take input num = int(input("Enter a number: ")) # Print result print("Reversed number:", reverse_number(num))Output:Enter a number: 123
Reversed number: 321How it works :
In this method we use two functions. one function is used to start the process and the other function performs the actual reversing.
The helper function works exactly like the previous recursion method. it processes one digit at a time and calls itself again.
The main function simply calls the helper function with the initial value.
This method makes the code easier to reuse and understand in bigger programs.
Methods summary table :
Method Technique Level Best For Recursion Function calls Intermediate Interviews Recursion + Sign Real case Important Practical Helper Function Clean code Intermediate Reusability Key points to remember :
- Recursion means function calling itself
- Base condition is very important
- % 10 gives last digit
- // 10 removes last digit
- Each call processes one digit
You can also read this:Frequently asked questions :Q. What is recursion?A) A function calling itselfQ. Which method is best?A) Basic recursion methodQ.Is recursion important?A) YesQ. Why use recursion?A) To solve problems step by stepConclusion :Reversing a number using recursion looks difficult at first time, but once you understand how eachfunction call works it becomes very simple.This problem is very useful for understanding recursion and improving your problem-solving skills.Practice this program step by step and try to trace how the function calls itself.That's all for today friends.Keepcoding,keep learning.DailyCodeHub

No comments:
Post a Comment